\documentclass[Orbiter Technical Reference.tex]{subfiles}
\begin{document}

\section{Object lighting}
\textbf{Martin Schweiger}\\
\textbf{January 26, 2006}


\subsection{Introduction}
This note contains some implementation details of lighting objects such as spacecraft or buildings on planet surfaces in Orbiter.

\subsection{Planetary shadows}
Planets and moons cast shadows into space away from the central sun. An object entering that shadow (such as an orbiting spacecraft, or a surface building moving across the planet's terminator) need to modify their lighting parameters to simulate this. In general, the planet shadow reduces the direct (diffuse and specular) lighting component, $D_\text{RGB}$. If the planet also contains an atmosphere, this can lead to a spectral dispersion of the direct light distribution in the twilight zone (penumbra), as well as an increase in the ambient component, $A_\text{RGB}$, due to atmospheric scattering. In the following, subscript ``RGB'' denotes a colour value consisting of red, green and blue components.

\subsubsection{Planets without atmosphere}
We start with the simpler case of a planet without atmospheric layer. In this case the ambient component is assumed to be unaffected (backscattering from the planet surface is currently not considered). The change in the direct component is given by geometric considerations, due to the planet disc covering part or all of the sun's disc.

Given a point in space $\vec{r}$ at which the lighting parameters are to be evaluated (e.g. the current position of a spacecraft, let $\vec{s}$ be the position of the sun, and $\vec{p}$ the position of the shadowing planet, both relative to $\vec{r}$.

With distances $s = |\vec{s}|$ and $p = |\vec{p}|$, the angular separation $\theta$ between sun and planet, as seen from $\vec{r}$, is given by
\begin{equation}
\theta = \cos^{-1} \frac{\vec{s} \cdot \vec{p}}{sp}
\end{equation}
Let $a_s$ and $a_p$ be the mean radii of sun and planet, respectively. Then their apparent radii, as seen from $\vec{r}$ are given by
\begin{equation}
\alpha_s = \sin^{-1} \frac{a_s}{s}, \qquad \alpha_p = \sin^{-1} \frac{a_p}{p}
\end{equation}
It follows that shadowing at $\vec{r}$ will only occur if
\begin{equation}
p < s \quad \text{and} \quad \theta < \alpha_s + \alpha_p
\end{equation}
Depending on the distance $p$ from the planet, we can divide the shadow zone into two areas: the near-zone, in which totality can occur (the planet disc completely covers the sun's disc), and the far zone, in which annularity can occur (the planet disc is entirely within the sun's disc).
The relevant zone can simply be determined by comparing the relative disc sizes:\begin{equation}\label{eq:shadowzone}
\begin{array}{lcl}
\text{case 1:} & \alpha_p \geq \alpha_s: & \text{totality zone}\\
\text{case 2:} & \alpha_p < \alpha_s: & \text{annularity zone}
\end{array}
\end{equation}

\paragraph{Case 1: totality zone}
If the object is within totality distance, we have to detemine if it is actually located within the cone of totality (umbra), or in the zone of partial shadow (penumbra):
\begin{equation}
\begin{array}{lcl}
\text{case 1a:} & \theta \leq \alpha_p-\alpha_s: & \text{totality}\\
\text{case 1b:} & \theta > \alpha_p-\alpha_s: & \text{partiality}
\end{array}
\end{equation}
In case 1a, direct lighting is eliminated. In case 1b, a linear propagation from full lighting to full shadow is assumed:
\begin{equation}\label{eq:partial1b}
\begin{array}{l}
D_\text{RGB}^{(\text{1a})} = (0,0,0)\\
D_\text{RGB}^{(\text{1b})} = (f,f,f) \quad\text{with}\quad f = \frac{\theta+\alpha_s-\alpha_p}{2 \alpha_s}
\end{array}
\end{equation}
The linear assumption is not correct, but probably not critical, and may partly be justifiable by physiological arguments (the eye is more sensitive at low light levels, therefore intensity should drop more gradually).
Note that in Eq.~\ref{eq:partial1b} the fully lit direct light level is assumed to be $D_\text{RGB}^{(0)} = (1,1,1)$, but different unperturbed base line levels can easily be multipled on.

\paragraph{Case 2: annularity zone}\label{sec:noatm_annul}
If the object is too far from the planet to enter the totality zone, we need to determine if the planet disc is contained fully or partially in the sun's disc:
\begin{equation}
\begin{array}{lcl}
\text{case 2a:} & \theta \leq \alpha_s-\alpha_p: & \text{annularity}\\
\text{case 2b:} & \theta > \alpha_s-\alpha_p: & \text{partiality}
\end{array}
\end{equation}
In case 2a, the light level is reduced according to the ratio of the two disc areas, while in case 2b a linear transition is assumed:
\begin{equation}
\left.
\begin{array}{l}
D_\text{RGB}^{(\text{2a})} = (f_0,f_0,f_0)\\
D_\text{RGB}^{(\text{2b})} = (f,f,f)
\end{array}
\right\rbrace
\begin{array}{l}
\text{with}\; f_0 = 1-(\alpha_p/\alpha_s)^2\\
\text{and}\; f = 1-\frac{1-f_0}{2} (1+\frac{\alpha_s-\theta}{\alpha_p})
\end{array}
\end{equation}
Note that for large distances the effect of the planet disc becomes insignificant. Orbiter disregards shadowing at distances where $\alpha_p/\alpha_s < 0.1$.

\subsubsection{Planets with atmospheric layer}
In the presence of an atmosphere, additional effects such as spectral filtering of the direct component and ambient in-scattering must be taken into account.
At high altitudes $h=p-a_p$, we can describe the atmospheric layer as an increase of the planet disc's radius of influence:
\begin{equation}\label{eq:altmod1}
\alpha_p' = \alpha_p \frac{h_0 + a_p}{a_p},\qquad (h > h_0)
\end{equation}
where $h_0$ is the effective thickness of the atmospheric layer (in Orbiter given by the AtmAlt parameter).
At low altitudes, when the object is already within the atmosphere, this value is no longer adequate. We simply assume that at ground level the atmosphere affects the lighting parameters up to an elevation $\gamma$ of the sun of $\gamma_0=10$\,deg. above the horizon, and interpolate to the actual altitude:
\begin{equation}\label{eq:altmod2}
\alpha_p' \leftarrow \alpha_p' \frac{\alpha_0}{\alpha_p'+\frac{h}{h_0}(\alpha_0-\alpha_p')},\qquad (0 \leq h \leq h_0)
\end{equation}
where $\alpha_0 = \gamma_0+\pi/2$, and $\alpha_p'$ on the right-hand side of Eq.~\ref{eq:altmod2} is taken from Eq.~\ref{eq:altmod1}.
Now, lighting parameters will be modified if
\begin{equation}
p < s \quad\text{and}\quad \phi < \alpha_s + \alpha_p'
\end{equation}
Again we can distinguish the two cases of Eq.~\ref{eq:shadowzone}.

\paragraph{Case 1: totality zone}
For totality, we set $D_\text{RGB}^{(\text{1a})} = (0,0,0)$ as before.
However, in the case of partial shadow, we now take into account that we lose more of the blue direct component due to scattering. The amount of spectral dispersion is linked to the atmospheric density $\rho_0$ at ground level, with the following ad-hoc formulae:
\begin{eqnarray}
\Delta &=& \ln (\rho_0+1),\quad\text{bounded at}\quad 0.02 \leq \Delta \leq 0.9\\
\Delta_\text{RGB} &=& 1-\left(
\begin{array}{c} 0.3 \\ 0.85 \\ 1 \end{array}
\right) \Delta
\end{eqnarray}
Now we use a piecewise linear transition:
\begin{equation}
D_\text{RGB}^{(\text{1b})} = \left\lbrace
\begin{array}{cll}
f (1-\Delta_\text{RGB}) + \Delta_\text{RGB} &\text{where}\; f = \frac{\theta-\alpha_s-\alpha_p}{\alpha_p'-\alpha_p} &\text{if}\; \theta > \alpha_s+\alpha_p \\
f \Delta_\text{RGB} & \text{where}\;f = \frac{\theta-\alpha_p+\alpha_s}{2\alpha_s} &\text{otherwise}
\end{array}
\right.
\end{equation}
Here, the top equation describes the case that the sun is fully above the horizon, and propagates from full lighting (1,1,1) to maximum dispersion. The bottom equation describes the case of the sun is partially covered by the horizon, and propagates from the dispersed state to darkness (0,0,0).

\paragraph{Case 2: annularity zone}
At distances beyond the totality zone atmospheric effects are considered negligible. Lighting parameters are calculated as described in Section~\ref{sec:noatm_annul}.

\paragraph{Ambient lighting}
In addition to changes to the direct light parameters, the atmosphere can also affect the ambient level due to atmospheric scattering, thereby increasing ambient light parameters during daytime.

Ambient lighting is considered to be affected when the sun's horizon elevation, $\gamma = \phi-\alpha_p$, is higher than a threshold level $\gamma_0$. In Orbiter, $\gamma_0 = -14$\,deg. universally. Ambient lighting is considered fully saturated for $\gamma > \gamma_1$ where $\gamma_1$ is another threshold value. In Orbiter, $\gamma_1 = +6$\,deg. universally.

The following ad-hoc formula is used to determine the ambient level $A$ as a functioion of atmospheric density at zero altitude, $\rho_0$:
\begin{equation}
\begin{array}{ll}
f = \frac{\gamma-\gamma_0}{\gamma_1-\gamma_0}, & \text{bounded at}\; f \leq 1\\
A_0 = 0.45 \ln(\rho_0+1), & \text{bounded at}\; A_0 \leq 0.7.\\
A = \frac{f A_0}{h/h_0 +1} &
\end{array}
\end{equation}
where $h$ is the altitude of the object, and $h_0$ is a reference altitude. In Orbiter, $h_0 = 2\cdot10^4$\,m universally. Thus, atmospheric effects are inversely proportional to altitude. Furthermore, ambient effects are not considered if $h$ is above the effective atmospheric layer defined by the AtmAlt parameter.

Ambient lighting modifications are non-dispersive, i.e. affect all channels equally: $A_\text{RGB} = (A,A,A)$.

Note that any user-defined ambient level is a universal additive component added to all objects at all times, on top of the individual atmospheric ambient effects described above. This will generally be used to increase the base line brightness of unlight surfaces.

\end{document}
